package org.vectrics.recruiter.struts.tag.job;

import java.util.List;

import org.apache.log4j.Logger;
import org.vectrics.company.CompanyPerson;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.struts.tag.cv.PersonLanguagesViewTag;
import org.vectrics.struts.VectricsTagSupport;

/**
 * Tag que permite Agregar como candidato de una "Job Order" 
 * la persona pasada como parametro.
 *  
 * @author pgoette
 */
public class AddInterviewTag extends VectricsTagSupport{

	private static final long serialVersionUID = -6966139307267042908L;
	static private transient Logger log = Logger.getLogger(PersonLanguagesViewTag.class);
	private String personId = "";
	private String companyId = "";
	
	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getPersonId() {
		return personId;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
		log.debug("setPersonId() - set to" + personId);
	}
	
	
	@Override
	protected String makeStartTag() {
		log.debug("AddInterview.makeStartTag() called");
		String html = "";
		// Si el id es nulo muestro un mensaje de error
		if (this.getPersonId() == null) {
			html = "<div class='stdFont'> "+ this.getPhrase("error.interview.add") +" </div>"; 
		} else {
			
			CompanyPerson employ =  RecruitServiceLocator.getCompanyService().findEmployeeForIdPerson(new Long(getPersonId())); 
			
			if (employ != null) {
				return html;
			}
			
			// Recupero la lista de Jobs
			List<Job> jobs= RecruitServiceLocator.getJobProvider().findJobsForInterview(new Long(this.getCompanyId()), new Long(getPersonId()));
			if ((jobs!=null)&&(!jobs.isEmpty())) {
				// Genero el formulario
				html += "<form method='POST' action='/hrs-intranet/admin/jobs/candidate/interview/edit.do'>";
				// Creo la tabla
				html += " <table border=0 > ";
				html += " <tr> ";
				html += " <td alingh='center' valingh='middle' > ";
				// Agrego el personId
				html += "<input type='hidden' name='personId' value='"+getPersonId()+"' />";
				html += "<input type='hidden' name='userId' value='"+getPersonId()+"' /> ";
				html += "<input type='hidden' name='companyId' value='"+ getCompanyId() +"' /> ";
				// Creo el select que contiene los Jobs 
				html += " <span class='subFont'>";
				html += this.getPhrase("prompt.interview.job");
				html += " </span>";
				html += " <select id='jobId' name='jobId'  style=' width:280px; ' >";
				int aux =0;
				
				for (Job job : jobs) {
					if (aux==0) {
						html += "<option value='"+job.getId().toString()+"' selected='selected'> "+ job.getReferenceId()+ " - " + job.getTitle() +"</option>";
						aux++;
					} else {
						html += "<option value='"+job.getId().toString()+"'> "+ job.getReferenceId()+ " - "+ job.getTitle()+"</option>";						
					}
				}
				html += " </select>";
				html += "</td> <td>";
		        html+="<input type=\"image\" ";
		        
		        String caption = this.getPhrase("prompt.interview.add");
		        String imageSource = this.getContextPath() + "/dynagraphic/button.jpg?code=button.param" 
		        	+ "&caption=" + caption;
		 
		        html+=" src=\"";
		        html+=imageSource;
		        html+="\" border=\"0\" ";
		        html+="value=\"";
		        html+="submit";
		        html+="\" ";
		        html+=" name=\"";
		        html+="submit";
		        html+="\" ";
		        html+=" /> ";
		        html += "</td>";
		        html += "</tr>";
		        html += "</table>";
				html += "</form>";
			} else {
				// Creo la tabla
				html += " <table border=0> ";
				html += " <tr> ";
				html += " <td alingh='center' valingh='middle' > ";
				// Creo el select que contiene los Jobs 
				html += " <span class='subFont'>";
				html += this.getPhrase("prompt.interview.job");
				html += " </span>";
				html += " <select id='jobId' name='jobId' style=' width:280px; ' disabled='disabled' >";
				html += " </select>";
				html += "</td> <td>";
				html+="<input type=\"image\" ";
		        String caption = this.getPhrase("prompt.interview.add");
		        String imageSource = this.getContextPath() + "/dynagraphic/button.jpg?code=button.param" 
		        	+ "&caption=" + caption;
		        html+=" src=\"";
		        html+=imageSource;
		        html+="\" border=\"0\" ";
		        html+=" /> ";
		        html += "</td>";
		        html += "</tr>";
		        html += "</table>";
			} 
		}
		return html;
	}

}
